package com.art.sunflower.service.impl;

import com.art.sunflower.enumeration.RoleType;
import com.art.sunflower.exception.BusyException;
import com.art.sunflower.exception.UsernameExistException;
import com.art.sunflower.mapper.TeacherMapper;
import com.art.sunflower.mapper.UserMapper;
import com.art.sunflower.pojo.User;
import com.art.sunflower.service.UserService;
import com.art.sunflower.util.Md5SaltTool;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    TeacherMapper teacherMapper;

    @Override
    public String login(String username, String password) throws AccountException {
        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken(username, password));
        User user = (User) subject.getPrincipal();
        return String.valueOf(user.getRole());
    }

    @Override
    public void register(User user) throws UsernameExistException, NoSuchAlgorithmException {
        //尝试获取同名用户
        if (userMapper.selectUser(user.getUsername()) != null)
            throw new UsernameExistException();
        else {
            //密码加密
            user.setPassword(Md5SaltTool.getEncryptedPwd(user.getPassword()));
            userMapper.insertUser(user);
        }
    }

    @Override
    public void resetPassword(String oldOne, String newOne) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (!Md5SaltTool.validPassword(oldOne, user.getPassword()))
            throw new IncorrectCredentialsException();
        try {
            user.setPassword(Md5SaltTool.getEncryptedPwd(newOne));
            userMapper.updatePassword(user.getUsername(), user.getPassword());
            subject.logout();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new BusyException();
        }
    }

    @Override
    public void updateInformation(String realName, String email) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        userMapper.updateRealName(user.getUsername(), user.getRealName());
        user.setRealName(realName);
        if (user.getRole() == RoleType.teacher) {
            teacherMapper.updateEmail(user.getId(), email);
        }
    }

    @Override
    public Map<String, Object> getInformation() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();

        Map<String, Object> map = new HashMap<>();
        map.put("username", user.getUsername());
        map.put("realName", user.getRealName());
        map.put("phone", user.getPhone());
        if (user.getRole() == RoleType.teacher) {
            map.put("email", teacherMapper.selectEmail(user.getId()));
        }
        return map;
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser1(user);
    }

}
